//
//  RegisterViewController.m
//  newDouban
//
//  Created by dllo on 15/10/17.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "RegisterViewController.h"
#import "DataBaseSingTon.h"
#import "User.h"

@interface RegisterViewController ()

@property (nonatomic, retain) NSMutableArray *textFieldArr;

@end

@implementation RegisterViewController

- (void)dealloc
{
    [_textFieldArr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
   // self.navigationController.title = @"注册";//在此,用这个方法不显示..注意??
    
    self.title = @"注册";
    
    NSArray *labelArr = [NSArray arrayWithObjects:@"用户名:", @"密码:", @"确认密码:", @"邮箱:", @"联系方式:", nil];
    
    NSArray *textfieldArr = [NSArray arrayWithObjects:@"请输入用户名", @"请输入密码", @"请输入确认密码", @"请输入邮箱", @"请输入联系方式", nil];
    
    for (NSInteger i = 0; i < labelArr.count; i++) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 100 + 60 * i , 100, 40)];
        label.text = [labelArr objectAtIndex:i];
        //label.backgroundColor = [UIColor orangeColor];
        label.font = [UIFont boldSystemFontOfSize:22.0];
        [self.view addSubview:label];
        [label release];
    }
    
    self.textFieldArr = [NSMutableArray array];
    for (NSInteger i = 0; i < textfieldArr.count; i++) {
        UITextField *textfield = [[UITextField alloc] initWithFrame:CGRectMake(20 + 100 + 14, 100 + 60 * i, 260, 40)];
        textfield.placeholder = [textfieldArr objectAtIndex:i];
        textfield.borderStyle = UITextBorderStyleBezel;
        //textfield.backgroundColor = [UIColor grayColor];
        textfield.font = [UIFont boldSystemFontOfSize:22.0];
        if (1 == i || 2 == i) {
            textfield.secureTextEntry = YES;
            textfield.keyboardType = UIKeyboardTypeNumberPad;
        }
        [self.textFieldArr addObject:textfield];
        [self.view addSubview:textfield];
        [textfield release];
        
    }
    
    
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(registerAction:)] autorelease];
    
    
    [[DataBaseSingTon shareDataBase] createUserTable];
}


- (void)UIAlertController:(NSString *)str
{
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];
    //注意:假如下面要加textfield要用UIAlertControllerStyleAlert
    
    [self presentViewController:alertC animated:YES completion:^{
        
        
    }];
    
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
    
//    //创建提示按钮
//    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        //block回调
//        NSLog(@"点击的是确定按钮");
//        
//    }];
//    
//    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        //block回调
//        NSLog(@"点击的是取消按钮");
//        
//    }];
//    
//    //将按钮放在提示框上
//    [alertC addAction:action];
//    [alertC addAction:action1];
}




- (void)registerAction:(UIBarButtonItem *)sender
{
    
    UITextField *userN = [self.textFieldArr objectAtIndex:0];
    UITextField *password = [self.textFieldArr objectAtIndex:1];
    
    NSString *userName = userN.text;
    NSString *passWord = password.text;
    
    
    User *user = [[User alloc] init];
    user.userName = userN.text;
    user.passWord = password.text;
    user.result = @"0";
    
    //NSString *sql = [NSString stringWithFormat:@"SELECT * FROM user WHERE userName = '%@' AND passWord = '%@'", user.userName, user.passWord];
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM user"];
    NSMutableArray *arrM = [[DataBaseSingTon shareDataBase] selectUserSQL:sql];
    
    
    BOOL result1 = [arrM containsObject:user.userName];
    
    NSLog(@"result1 == %d", result1);
    
    if (!result1) {
        [[DataBaseSingTon shareDataBase] insertUser:user];
        [self.navigationController popViewControllerAnimated:YES];
        
    } else {
        [self UIAlertController:@"已经注册过了"];
    }
    
    
    
//    NSLog(@"userName = %@, passWord = %@", userName, passWord);
//    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:userName forKey:@"userName"];
    [userDefaults setObject:passWord forKey:@"passWord"];
    [userDefaults synchronize];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
